package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_user_role_action;

import cn.com.api.service.ApiT_user_role_actionService;

import cn.com.util.*;
import cn.com.util.*;


public class T_user_role_actionBaseController {

	private static Logger logger = Logger.getLogger(T_user_role_actionBaseController.class);
	
	@Autowired
	private ApiT_user_role_actionService apiT_user_role_actionService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "t_user_role_action-list")
	public ModelAndView t_user_role_action_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_user_role_action_list.jsp");
		
		List<ApiT_user_role_action> t_user_role_actions = apiT_user_role_actionService.findT_user_role_actions();
		mv.addObject("t_user_role_actions", t_user_role_actions);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "t_user_role_action-add-pre")
	public ModelAndView t_user_role_action_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_user_role_action_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiT_user_role_action t_user_role_action = apiT_user_role_actionService.findT_user_role_actionById(Integer.valueOf(id));
			mv.addObject("t_user_role_action",t_user_role_action);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "t_user_role_action-add")
	public void t_user_role_action_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
	
		String role_id = request.getParameter("role_id");
		String action_id = request.getParameter("action_id");
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiT_user_role_action t_user_role_action = new ApiT_user_role_action();
            /*
		    t_user_role_action.setId(id);
		    t_user_role_action.setRole_id(role_id);
		    t_user_role_action.setAction_id(action_id);
	        */
			apiT_user_role_actionService.insertT_user_role_action(t_user_role_action);
		}
		//edit
		else{
			ApiT_user_role_action t_user_role_action = apiT_user_role_actionService.findT_user_role_actionById(Integer.valueOf(id));
	        /*
		    t_user_role_action.setId(id);
		    t_user_role_action.setRole_id(role_id);
		    t_user_role_action.setAction_id(action_id);
			*/
			apiT_user_role_actionService.updateT_user_role_action(t_user_role_action);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "t_user_role_action-delete")
	public void t_user_role_action_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiT_user_role_actionService.deleteT_user_role_action(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
}



